<template>
  <div>
    <el-form :model="form" label-width="auto" inline>
      <el-form-item label="档口名称：">
        <el-input v-model="form.name" />
      </el-form-item>
      <el-form-item label="状态：">
        <el-select v-model="form.isUse">
          <el-option label="全部" :value="-1" />
          <el-option label="启用中" :value="1" />
          <el-option label="禁用中" :value="0" />
        </el-select>
      </el-form-item>
      <el-form-item label="档口号：">
        <el-input v-model="form.num" />
      </el-form-item>
      <el-form-item label="所属仓库:">
        <el-select v-model="form.warehouseId">
          <el-option :label="item.name" :value="item.id" v-for="item in store.NewHouseList" :key="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">搜索</el-button>
        <el-button type="primary" @click="createStall('新增')">新增</el-button>
      </el-form-item>
    </el-form>
  </div>
  <div class="table">
    <el-table ref="multipleTableRef" :data="tableData" style="width: 100%" @selection-change="handleSelectionChange"
      border>
      <el-table-column type="selection" width="55" />
      <el-table-column label="档口号">
        <template #default="scope">{{ scope.row.num }}</template>
      </el-table-column>
      <el-table-column label="所属仓库" width="180">
        <template #default="scope">{{ scope.row.warehouseName }}</template>
      </el-table-column>
      <el-table-column label="档口名称" width="180">
        <template #default="scope">{{ scope.row.name }}</template>
      </el-table-column>
      <el-table-column label="档口电话" width="180">
        <template #default="scope">{{ scope.row.phone }}</template>
      </el-table-column>
      <el-table-column label="档口地址" width="380">
        <template #default="scope"><span style="color: red;">{{ scope.row.address }}</span></template>
      </el-table-column>
      <el-table-column label="是否启用" width="180">
        <template #default="scope">{{ scope.row.isUse == 1 ? '是' : '否' }}</template>
      </el-table-column>
      <el-table-column label="备注">
        <template #default="scope">{{ scope.row.remark }}</template>
      </el-table-column>
      <el-table-column label="操作" width="180">
        <template #default="scope">
          <el-button link type="primary" size="small" @click.prevent="setStall('编辑', scope.row)">
            编辑
          </el-button>
          <el-button link type="primary" size="small" @click.prevent="delectRow(scope.row)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
  <div class="page">
    <el-pagination v-model:current-page="currentPage4" v-model:page-size="pageSize4" :page-sizes="[10, 50, 100]"
      layout="total, sizes, prev, pager, next, jumper" :total="total" @size-change="handleSizeChange"
      @current-change="handleCurrentChange" />
  </div>
  <div>
    <el-dialog v-model="dialogVisible" width="500">
      <span>
        <el-form :model="Createform">
          <el-form-item label="所属仓库:">
            <el-select v-model="Createform.warehouseId">
              <el-option :label="item.name" :value="item.id" v-for="item in store.NewHouseList" :key="item.id" />
            </el-select>
          </el-form-item>
          <el-form-item label="档口号：">
            <el-input v-model.number="Createform.num" style="width: 220px;" />
          </el-form-item>
          <el-form-item label="档口名称：">
            <el-input v-model="Createform.name" style="width: 220px;" />
          </el-form-item>
          <el-form-item label="档口电话：">
            <el-input v-model="Createform.phone" style="width: 220px;" />
          </el-form-item>
          <el-form-item label="档口地址：">
            <el-input v-model="Createform.address" />
          </el-form-item>
          <el-form-item label="状态：">
            <div>
              <label>
                <input type="radio" :value="1" v-model="Createform.isUse"> 启用
              </label>
              <label>
                <input type="radio" :value="0" v-model="Createform.isUse"> 禁用
              </label>
            </div>
          </el-form-item>
          <el-form-item label="备注：">
            <el-input v-model="Createform.remark" type="textarea" />
          </el-form-item>
        </el-form>
      </span>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogVisible = false, Createform = {}">取消</el-button>
          <el-button type="primary" @click="submitPlatformType">
            保存
          </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onBeforeMount } from 'vue'
import { useAlertsStore } from '@/store/index'
import { gearOpenlist, gearOpenCreate, gearOpenupdate, gearOpendelete } from '@/api/index'
import { ElMessage } from 'element-plus'
const store = useAlertsStore()
const form = ref({
  name: '',
  isUse: -1,
  num: '',
  warehouseId: -1,
  pageSize: 10,
  pageNum: 1
})
const tableData = ref([])
onBeforeMount(() => {
  store.wareHouseSelect()
  getList()
})
const onSubmit = () => {
  getList()
}
const getList = () => {
  gearOpenlist(form.value).then(res => {
    console.log('调用接口')
    // console.log(res)
    const { data: { data } } = res
    total.value = data.total
    tableData.value = data.list
  })
}
// table
const multipleSelection = ref([])
const handleSelectionChange = (val) => {
  multipleSelection.value = val
}
// 分页
const currentPage4 = ref(1)
const pageSize4 = ref(10)
const total = ref(0)
const handleSizeChange = (val) => {
  console.log(`${val} items per page`)
  form.value.pageSize = val
  getList()
}
const handleCurrentChange = (val) => {
  console.log(`current page: ${val}`)
  form.value.pageNum = val
  getList()
}
// 新增
const dialogVisible = ref(false)
const Createform = ref({})
const typeName = ref(null)
const createStall = (name) => {
  dialogVisible.value = true
  typeName.value = name
}
const submitPlatformType = () => {
  if (typeName.value === '新增') {
    gearOpenCreate(Createform.value).then(res => {
      ElMessage.success('操作成功')
      dialogVisible.value = false
      getList()
    })
  } else if (typeName.value === '编辑') {
    gearOpenupdate(Createform.value).then(res => {
      ElMessage.success('操作成功')
      dialogVisible.value = false
      getList()
    })
  }
}
const setStall = (name, row) => {
  Createform.value = row
  dialogVisible.value = true
  typeName.value = name
}
const delectRow = (row) => {
  gearOpendelete({ id: row.id }).then(res => {
    ElMessage.success('操作成功')
    getList()
  })
}
</script>

<style lang='less' scoped></style>
